<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/4/17
 * Time: 下午4:37
 */

namespace App\Logic;

use App\Server\ApplyModel;
use App\Server\CollectionModel;
use App\Server\FileUrlModel;
use App\Server\PartTimeJobModel;

/**
 * 我的
 *
 * Class MineLogic
 * @package App\Logic
 */
class MineLogic extends BaseLogic
{

    /**
     * 收藏
     *
     * @param int $id
     * @param int $userId
     * @return array
     */
    public static function collect($id = 0, $userId = 0)
    {
        if ($id && $userId) {
            $detail = CollectionModel::first(['member_id' => $userId, 'p_id' => $id, 'is_delete' => 0]);
            if ($detail) {
                $up = CollectionModel::updated(['id' => $detail['id']], ['is_delete' => 1, 'delete_time' => time()]);
                if ($up) {
                    return self::success(['flag' => 0], 200, '取消收藏成功');
                } else {
                    return self::error('取消收藏失败', 206);
                }
            } else {
                $data = [];
                $data['member_id'] = $userId;
                $data['p_id'] = $id;
                $data['create_time'] = time();
                $data['update_time'] = time();
                $up = CollectionModel::add($data);
                if ($up) {
                    return self::success(['flag' => 1], 200, '收藏成功');
                } else {
                    return self::error('收藏失败', 206);
                }
            }
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    /**
     * 收藏列表
     *
     * @param $params
     * @param $userId
     * @return array
     */
    public static function getList($params, $userId)
    {
        if ($params && $userId) {
            $pagesize = isset($params['pagesize']) ? $params['pagesize'] : 10;
            $lastId = isset($params['last_id']) ? $params['last_id'] : 1;
            if (isset($params['type']) && $params['type'] > 0) {
                $so['part_time_job.is_part'] = $params['type'];
            }
            $so['collection.is_delete'] = 0;
            $so['part_time_job.is_delete'] = 0;
            $detail = CollectionModel::getListLikeMemberList($pagesize, null, $lastId, $so);
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $result = [];
                    foreach ($list as $k => $v) {
                        $item = [];
                        $item['id'] = $v['id'];
                        $item['title'] = $v['title'];
                        $item['keywords'] = $v['keywords'];
                        $item['content'] = $v['content'];
                        $item['time'] = $v['time'];
                        $item['address'] = $v['address'];
                        $item['salary'] = $v['salary'];
                        $item['number'] = $v['number'];
                        $item['welfare'] = $v['welfare'];
                        $item['company_id'] = $v['company_id'];
                        $item['company_name'] = $v['company_name'];
                        $item['company_grade'] = $v['company_grade'];
                        $item['create_time'] = $v['create_time'];
                        $item['image_url'] = self::getImageUrl($v['id']);
                        $result[] = $item;
                    }
                    $detail['data'] = $result;
                }
            }
            return self::success($detail, 200, 'ok');
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    /**
     * 获取主图片
     *
     * @param int $id
     * @return string
     */
    private static function getImageUrl($id = 0)
    {
        if ($id) {
            $firstImageUrl = '';
            $detail = FileUrlModel::getList(10, null, 1, ['related_id' => $id, 'is_delete' => 0]);
            if ($detail) {
                $imageList = $detail['data'];
                if ($imageList) {
                    $firstImage = $imageList[0];
                    $firstImageUrl = $firstImage['file_url'];
                }
            }
            return $firstImageUrl;
        } else {
            return '';
        }
    }

    /**
     * 申请
     * @param $params
     * @return array
     */
    public static function apply($params)
    {
        if ($params) {
            $detail = ApplyModel::first($params);
            if ($detail) {
                return self::error(206, '请勿重复申请');
            }
            $params['create_time'] = time();
            $params['update_time'] = time();
            $up = ApplyModel::add($params);
            if ($up) {
                return self::success(['flag' => 1], 200, '报名成功');
            } else {
                return self::error('报名失败', 206);
            }
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    /**
     * 报名列表
     *
     * @param array $params
     * @param int $userId
     * @return array
     */
    public static function applyList($params = [], $userId = 0)
    {
        if ($params) {
            $detail = ApplyModel::getList($params['pagesize'], null, $params['last_id'], ['member_id' => $userId]);
            if ($detail) {
                $list = $detail['data'];
                $result = [];
                if ($list) {
                    foreach ($list as $k => $u) {
                        $jobId = $u['job_id'];
                        $v = PartTimeJobModel::first(['id' => $jobId]);
                        $item = [];
                        $item['id'] = $v['id'];
                        $item['title'] = $v['title'];
                        $item['keywords'] = $v['keywords'];
                        $item['content'] = $v['content'];
                        $item['time'] = $v['time'];
                        $item['address'] = $v['address'];
                        $item['salary'] = $v['salary'];
                        $item['number'] = $v['number'];
                        $item['welfare'] = $v['welfare'];
                        $item['company_id'] = $v['company_id'];
                        $item['company_name'] = $v['company_name'];
                        $item['company_grade'] = $v['company_grade'];
                        $item['create_time'] = $v['create_time'];
                        $item['image_url'] = self::getImageUrl($v['id']);
                        $result[] = $item;
                    }
                    $detail['data'] = $result;
                }
            }
            return self::success($detail, 200, 'ok');
        } else {
            return self::error('请求参数有误', 206);
        }
    }
}